package com.qianbao.finance.sync.modules.elasticsearch.websocket.utils;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

/**
 * CByteDecoder
 * 
 * @author WangYuan
 * @date   2016年7月23日下午3:42:28
 */
public class CByteDecoder extends FrameDecoder{

    private static final Logger logger = Logger.getLogger(CByteDecoder.class);

    @Override
    protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel,
            ChannelBuffer channelBuffer) throws Exception {
        if (channelBuffer.readableBytes() < 14) {
            logger.info("decode one channelbuffer lt 14");
            return null;
        }
        byte[] len = new byte[8];
        channelBuffer.getBytes(5, len, 0, 8);
        ArrayUtils.reverse(len);
        long contentLen = CodeUtil.bytes2Long(len);
        if (channelBuffer.readableBytes() < (13 + (int) contentLen)) {
            logger.info("decode one channelbuffer lt normal content");
            return null;
        }
        return channelBuffer.readBytes(13 + (int) contentLen);
    }
    
}
